<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport('joomla.application.component.controller');
  include('base.php');

  class VenueController extends BaseController
  {
    var $_name = 'venue';
    
    function display()
    {
      $document =& JFactory::getDocument();
      
      $view = & $this->getView($this->_name, $document->getType());
      $view->setModel($this->getModel($this->_name), true);
      $view->setModel($this->getModel('pic','picmodel'));
      
      $view->displayList();   
    }
    
    function edit()
    {
      $cids = JRequest::getVar('cid', null, 'default', 'array' ); 
    
      if($cids === null)
        JError::raiseError(500, 'cid parameter missing from the request');   
    
      $document =& JFactory::getDocument();
      
      $view = & $this->getView($this->_name, $document->getType());
      $view->setModel($this->getModel($this->_name), true);
      $view->setModel($this->getModel('pic','picmodel'));
      
      $view->displayEdit();  
    }
    
    function save()
    {
      $data = JRequest::get('POST');
      $data['address'] = JRequest::getVar( 'address', '', 'post', 'string', JREQUEST_ALLOWHTML );
      $data['google_map'] = JRequest::getVar( 'google_map', '', 'post', 'string', JREQUEST_ALLOWRAW );
      
      $pic = $this->getModel('pic','picmodel');
      $model = & $this->getModel($this->_name);
      
      $d_pic = $this->reconstruct_pic($data);
      $row = $pic->save($d_pic);
      $data['pic_id'] = $row->id;
      
      $model->save($data);
       
      $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
      $this->setRedirect($redirectTo,  ucfirst($this->_name) . ' Saved!');

    }
    
    private function reconstruct_pic($data){
      
	  $result = array();
      $result['id']          = $data['pic_id'];  
      $result['description'] = basename($data['pic_url']);
      $result['pic_type']    = 'VENUES';
      $result['pic_id']      = $data['id'];
      $result['flickr_id']   = $data['pic_flickr_id'];
	  $result['url']         = $data['old_pic_url'];
      $result['pic_url']     = $data['pic_url'];
	  
	  if($result['id'])
        $result['updated_on'] = date('Y-m-d',time());
      else
        $result['created_on'] = date('Y-m-d',time());
      
      return $result;
    }
  }
?>